<?php

namespace backend\models\course;
use backend\models\course\Course;
use backend\models\article\Article;
use backend\models\user\User;
use yii\data\Pagination;
use Yii;

/**
 * This is the model class for table "{{%edu_comment}}".
 *
 * @property integer $comment_id
 * @property integer $comment_type
 * @property integer $model_id
 * @property integer $comment_addtime
 * @property integer $user_id
 * @property string $user_name
 * @property string $comment_content
 * @property integer $comment_status
 */
class Comment extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%comment}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['comment_type', 'model_id', 'comment_addtime', 'user_id', 'comment_status'], 'integer'],
            [['comment_content'], 'string'],
			[ 'comment_addtime','default', 'value' =>time(),],
            [['user_name'], 'string', 'max' => 20]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'comment_id' => 'Comment ID',
            'comment_type' => 'Comment Type',
            'model_id' => 'Model ID',
            'comment_addtime' => 'Comment Addtime',
            'user_id' => 'User ID',
            'user_name' => 'User Name',
            'comment_content' => 'Comment Content',
            'comment_status' => 'Comment Status',
        ];
    }
	/**
	* 量表联查
	*/
	public function getComment()
    {
		return $this->hasMany(Course::className(), ['course_id'=>'model_id']);
	}

    /**
    * 量表联查
    */
    public function getUser()
    {
        return $this->hasMany(User::className(), ['user_id'=>'user_id']);
    }

    /**
    *   资讯的两表联查
    **/
    public function getArtcome(){
        return $this->hasMany(Article::className(),['article_id'=>'model_id']);
    }
	//总条数
	public function CommentCount($code)
    {
		return $this->find()
					->joinwith('comment')
					->where($code)
					->count();
	}
	/**
	* 搜索查询
	*/
	public function CommentSearch($code,$pagination)
    {
		return $this->find()
				->joinwith('comment')
				->where($code)
				->offset($pagination->offset)
				->limit($pagination->limit)
				->asArray()
				->all();
	}
	/**
	* 删除评论
	*/
	public function CommentDelete($comment_id)
    {
		return $this->deleteAll("comment_id in($comment_id)");
	}
	/**
	* 修改评论状态
	*/
	public function CommentUpdate($comment_id,$status)
    {
		return $this->updateAll(['comment_status' => $status],'comment_id=:comment',[':comment'=>$comment_id]);
	}
}
